﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[ExecuteInEditMode]
[AddComponentMenu("UI/Behavior/SliderPin")]
public class UISliderPin : MonoBehaviour
{

    public Slider slider;

    // Update is called once per frame
    void Update()
    {
        if (!slider) return;

        float x,y;
        switch (slider.direction)
        {
            case Slider.Direction.LeftToRight:
                x = slider.fillRect.position.x + slider.fillRect.rect.width / 2.0f;
                //slider  nil 问题
                transform.position = new Vector3(x, transform.position.y, transform.position.z);
                break;
            case Slider.Direction.RightToLeft:
                x = slider.fillRect.position.x - slider.fillRect.rect.width / 2.0f;
                transform.position = new Vector3(x, transform.position.y, transform.position.z);
                break;
            case Slider.Direction.BottomToTop:
                y = slider.fillRect.position.y+ slider.fillRect.rect.height / 2.0f;
                transform.position = new Vector3(transform.position.x,y, transform.position.z);
                break;
            case Slider.Direction.TopToBottom:
                y = slider.fillRect.position.y- slider.fillRect.rect.height / 2.0f;
                transform.position = new Vector3(transform.position.x,y, transform.position.z);
                break;
            default:
                break;
        }

    }
}
